﻿using System;
using System.Web;
using System.Web.Mvc;
using Kaiser.MvcBase.Models;
using Kaiser.MvcBase.Models.ViewContent;
using Kaiser.MvcBase.Resources;
using Kaiser.MvcBase.Service;

namespace Kaiser.MvcBase.Context {
    public  class ManageContext {

        public static void Init(IAuthenUserService userService) {
            _userService = userService;
        }
        private  static PageModel _pageModel;

        private static IAuthenUserService _userService;
        public static IAuthenUserService UserService
        {
            get { return _userService; }
        }

        private static  HttpContext Context {
            get { return HttpContext.Current; }
        }

        public  static UserModel User
        {
            get
            {
                if(Context==null)
                    throw new Exception(Messages.ManageContext_User_SessionNotFound);
                var session = Context.Session;
                if (session != null)
                {
                    var userModel = session[typeof(UserModel).FullName] as UserModel;
                    if (Context.Request.IsAuthenticated && userModel == null)
                    {
                        var model = _userService.FindUserModel(Context.User.Identity.Name);

                        if (model == null)
                        {
                            throw new Exception(Messages.ManageContext_User_NotFound);
                        }
                        session[typeof(UserModel).FullName] = userModel = model;
                    }
                    return userModel;
                }
                throw new Exception(Messages.ManageContext_User_SessionNotFound);
            }
        }

        public  static SiteModel Site {
            get {
                var siteModel = Context.Application[typeof (SiteModel).FullName] as SiteModel;
                if (siteModel == null) {
                    Context.Application[typeof (SiteModel).FullName] = siteModel = new SiteModel {
                        Name = string.Empty,
                        Keywords = string.Empty,
                        Description = string.Empty
                    };
                }

                return siteModel;
            }
        }

        public static  PageModel Page {
            get {
                return _pageModel ?? (_pageModel = new PageModel {
                    Title = Site.Name,
                    Keywords = Site.Keywords,
                    Description = Site.Description,
                    UrlReferrer =
                        Context.Request.UrlReferrer != null
                            ? Context.Request.UrlReferrer.ToString()
                            : string.Empty,
                });
            }
        }

        public Models.Route GetRoute(ViewContext context)
        {
            var obj = new Models.Route
            {
                Area = tryeGetRouteInfo("area", context),
                Controller = tryeGetRouteInfo("controller", context),
                Action = tryeGetRouteInfo("action", context),
            };
            return obj;
        }

        private  string tryeGetRouteInfo(string key, ViewContext context) {
            object value;
            if (context.RouteData.Values.TryGetValue(key, out value))
                return value.ToString().ToLower();
            if (context.RouteData.DataTokens.TryGetValue(key, out value))
                return value.ToString().ToLower();
            return string.Empty;
        }
    }
}